Completed
Push — master ( 605e3d...3de0bb )
by Muhammad Dyas
14s queued 13s
created

ClosePollFormCard.buildHeader   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
1
import BaseCard from './BaseCard';
2
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
3
import {LocaleTimezone, PollState} from '../helpers/interfaces';
4
import {generateHelperWidget} from '../helpers/helper';
5
6
export default class ClosePollFormCard extends BaseCard {
7
  id = 'close_poll_form';
8
  state: PollState;
9
  timezone: LocaleTimezone;
10
11
  constructor(config: PollState, timezone: LocaleTimezone) {
12
    super();
13
    this.state = config;
14
    this.timezone = timezone;
15
  }
16
17
  create(): chatV1.Schema$GoogleAppsCardV1Card {
18
    this.buildHeader();
19
    if (this.state.closedTime) {
20
      this.buildCurrentScheduleInfo();
21
    }
22
    this.buildButtons();
23
    this.buildSections();
24
    return this.card;
25
  }
26
27
  buildHeader() {
28
    this.card.header = {
29
      'title': 'Are you sure to close the poll?',
30
      'subtitle': 'No one will have the ability to vote.',
31
      'imageUrl': '',
32
      'imageType': 'CIRCLE',
33
    };
34
  }
35
36
  buildCurrentScheduleInfo() {
37
    const locale = this.timezone.locale;
38
    const closedDate = new Date(this.state.closedTime!).toLocaleString(locale, {timeZone: this.timezone.id});
39
    this.card.sections!.push(
40
      {
41
        'widgets': [
42
          {
43
            'decoratedText': {
44
              'text': `<i>This poll already has Auto Close schedule at <time> ${closedDate}</time>(${this.timezone.id}) </i>`,
45
              'startIcon': {
46
                'knownIcon': 'CLOCK',
47
                'altText': '@',
48
              },
49
            },
50
          },
51
        ],
52
      });
53
  }
54
55
  buildButtons() {
56
    let scheduleButtonText = 'Create Schedule Close';
57
    if (this.state.closedTime) {
58
      scheduleButtonText = 'Edit Schedule Close';
59
    }
60
61
    this.addSectionWidget({
62
      'buttonList': {
63
        'buttons': [
64
          this.createButton(scheduleButtonText, 'schedule_close_poll_form'),
65
          this.createButton('Close Now', 'close_poll'),
66
        ],
67
      },
68
    });
69
  }
70
71
  buildSections() {
72
    this.card.sections!.push(generateHelperWidget());
73
  }
74
}
75